#region Usings

using System;
using KanbanBoard.Client.Model.Query;

#endregion

namespace KanbanBoard.Client.ViewModel.TaskBoardUseCase {
    public class TaskViewModel : ViewModel {
        private readonly IContext context;
        private bool editMode;
        internal Task Task { get; private set; }
        public ColumnViewModel ColumnViewModel { get; set; }

        public TaskViewModel(Task task, ColumnViewModel columnViewModel, IContext context) {
            this.context = context;
            Task = task;
            ColumnViewModel = columnViewModel;
            editMode = task.Description == null;
        }

        public virtual string Description { get { return Task.Description; } set { Task.Description = value; } }
        public Action Edit { get { return OnEdit; } }
        public Action Save { get { return OnSave; } }
        public Action Delete { get { return OnDelete; } }
        public bool DisplayMode { get { return !editMode; } }

        public bool EditMode {
            get { return editMode; }
            private set {
                editMode = value;
                NotifyChanged(() => EditMode);
                NotifyChanged(() => DisplayMode);
                NotifyChanged(() => Description);
            }
        }

        private void OnEdit() {
            EditMode = true;
        }

        private void OnSave() {
            context.Command.ChangeTaskDescriptionAsync(Task.Id, Task.Description);
            EditMode = false;
        }

        private void OnDelete() {
            context.Command.DeleteTaskAsync(Task.Id);
        }

    }
}